export const formatDate = (dateString) => {
  if (!dateString) return '';
  try {
    const date = new Date(dateString);
    if (isNaN(date.getTime())) return 'Invalid Date';
    return date.toLocaleString();
  } catch (e) {
    return 'Invalid Date';
  }
};

export const formatDuration = (minutes) => {
  if (typeof minutes !== 'number' || isNaN(minutes)) return '';
  const hours = Math.floor(minutes / 60);
  const mins = minutes % 60;
  return hours > 0 ? `${hours}小时${mins}分钟` : `${mins}分钟`;
};

export const getAvatarColor = (name) => {
  if (!name) name = "Default";
  const colors = ['#1890ff', '#52c41a', '#722ed1', '#fa8c16', '#eb2f96'];
  let hash = 0;
  for (let i = 0; i < name.length; i++) {
    hash = name.charCodeAt(i) + ((hash << 5) - hash);
  }
  return colors[Math.abs(hash) % colors.length];
};

export const getStudentInitials = (name) => {
  if (!name || name.length === 0) return '?';
  return name.charAt(0).toUpperCase();
};

export const getStatusText = (status) => {
  switch (status) {
    case 'pending': return '待批改';
    case 'graded': return '已批改';
    case 'reviewed': return '已复核';
    default: return '未知';
  }
};

export const getStatusClass = (status) => {
  // These classes would correspond to your CSS
  switch (status) {
    case 'pending': return 'status-pending';
    case 'graded': return 'status-graded';
    case 'reviewed': return 'status-reviewed';
    default: return '';
  }
};

export const getExamStatusClass = (status) => {
  // These classes would correspond to your CSS
  switch (status) {
    case '进行中': return 'exam-status-ongoing';
    case '已结束': return 'exam-status-ended';
    case '未开始': return 'exam-status-pending';
    default: return '';
  }
};

// A simple deep clone function for our mock data
export const deepClone = (obj) => {
  if (obj === null || typeof obj !== 'object') {
    return obj;
  }
  if (Array.isArray(obj)) {
    return obj.map(deepClone);
  }
  const cloned = {};
  for (const key in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, key)) {
      cloned[key] = deepClone(obj[key]);
    }
  }
  return cloned;
};
